/*
 * @Author: StarAire 1573905390@qq.com
 * @Date: 2023-08-26 07:11:10
 * @LastEditors: StarAire 1573905390@qq.com
 * @LastEditTime: 2023-08-28 06:56:07
 * @FilePath: /snake_linux/input.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <errno.h>
#include "snake/input.h"
#include "snake/game.h"
#include "snake/global.h"

static struct termios old;

/**
 * @description: 恢复旧的设置 
 * @return {*}
 */
static void RestoreOld(void)
{
    int ret = 0;
    ret = tcsetattr(0,TCSANOW,&old);
    if(ret == -1)
    {
        perror("tcsetattr error");
        exit(-1);
    }
}

/**
 * @description: 输入模块初始化
 * @return {*}
 */
void InputInit(void)
{
    int ret = 0;
    struct termios new;
    ret = tcgetattr(0,&old); // 保存旧的设置
    if(ret == -1)
    {
        perror("tcgetattr error");
        exit(-1);
    }
    atexit(RestoreOld); // 程序结束的时候自动恢复
    ret = tcgetattr(0,&new);
    if(ret == -1)
    {
        perror("tcgetattr error");
        exit(-1);
    }
    new.c_lflag = new.c_lflag & ~(ICANON | ECHO); // 激活终端模式，去掉回显
    new.c_cc[VMIN] = 1; // 只能接收一个字符
    new.c_cc[VTIME] = 0; // 需要回车去掉
    ret = tcsetattr(0,TCSANOW,&new);
    if(ret == -1)
    {
        perror("tcsetattr error");
        exit(-1);
    }
}

void InputScan()
{
    int ch;
    // 按键扫描
    ch = getchar();
    if (ch == EOF)
    {
        if (errno == EINTR) // 再重入
        {
            return ;
        }
        else
        {
            perror("Error reading input");
            exit(-1);
        }
        
    }
    else
    {
        if(game_status == 1) // 游戏开始界面
        {
            switch (ch)
            {
                default:
                    game_status = 2;
                    break;
            }
            
        }
        else if(game_status == 2)
        {
            switch (ch)
            {
                case 27: // ecs
                    game_status = 1;
                    break;
                case 32: // 空格
                    // game_status = 3;
                    break;
                case 65: // 向上
                    if(mysnake.motion_dir == 0)
                    {
                        sudu = 1;
                    }
                    else
                    {
                        mysnake.motion_dir = 0;
                        sudu = 3;
                    }
                    
                    break;
                case 66: // 向下
                    if(mysnake.motion_dir == 1)
                    {
                        sudu = 1;
                    }
                    else
                    {
                        mysnake.motion_dir = 1;
                        sudu = 3;
                    }
                    // mysnake.motion_dir = 1;
                    break;
                case 67: // 向右
                    if(mysnake.motion_dir == 3)
                    {
                        sudu = 1;
                    }
                    else
                    {
                        mysnake.motion_dir = 3;
                        sudu = 3;
                    }
                    //mysnake.motion_dir = 3;
                    break;
                case 68: // 向左
                    if(mysnake.motion_dir == 2)
                    {
                        sudu = 1;
                    }
                    else
                    {
                        mysnake.motion_dir = 2;
                        sudu = 3;
                    }
                    // mysnake.motion_dir = 2;
                    break;
                default:
                    break;
            }
        }
        else if(game_status == 3)
        {
            switch (ch)
            {
                case 27: // ecs
                    game_status = 2;
                    break;
                case 32: // 空格
                    game_status = 2;
                    break;
                default:
                    break;
            }
        }
    }


}



void InputEnd(void)
{
    RestoreOld();
}